<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 给定一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，
        // 使得 a + b + c = 0 ？找出所有满足条件且不重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4]，

    function getSum(arr) {
      var count = 0;
      var a = arr[count];
      var b = null, c = null, newArr = [];
      while (count < arr.length-3) {
        for (var i = count+2; i < arr.length; i++) {
          for (var j = count+1; j < arr.length; j++) {
            if (a + arr[i] + arr[j] == 0) {
                b = arr[i];
                c = arr[j];
                //   newArr.push([a,b,c]);
                if(newArr.length==0){
                    newArr.push([a,b,c]);
                }
                // [
                //     [-1, 1, 0]
                //     [-1, 2, -1]
                // ]
                var isRight=true;//
                for(var k=0;k<newArr.length;k++){
                    var val=newArr[k];
                    var x=0;
                    if(val.indexOf(a)!=-1){
                        x+=1;
                    }
                    if(val.indexOf(b)!=-1){
                        x+=1;
                    }
                    if(val.indexOf(c)!=-1){
                        x+=1;
                    }
                    // console.log(x);
                    if(x==3){
                        // break;
                        isRight=false;
                        break;
                    }
                }
                if(isRight){
                    newArr.push([a,b,c]);
                    isRight=true;
                }
            }
          }
        }
        count++;
      }
      return newArr;
    }
    var arr = [-1, 0, 1, 2, -1, -4];
    console.log(getSum(arr));

        
    </script>
</body>
</html>